package theOrbalEra.common.network;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.idList.PacketId;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.util.MathHelper;
import net.minecraft.world.WorldServer;

import com.google.common.io.ByteArrayDataInput;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.PacketDispatcher;

public class PacketSpawnEntity extends PacketBase {
	public static PacketSpawnEntity instance=new PacketSpawnEntity();

	@Override
	void readClient(int id, ByteArrayDataInput data, Object[] extradata) {
		// TODO Auto-generated method stub

	}

	@Override
	void readServer(int id, ByteArrayDataInput data, Object[] extradata) {
		int entityId=data.readInt();
		double x=data.readDouble();
		double y=data.readDouble();
		double z=data.readDouble();
		EntityPlayer player=(EntityPlayer) extradata[0];
		 WorldServer worldserver = FMLCommonHandler.instance().getMinecraftServerInstance().worldServerForDimension(player.dimension);
	     Entity entity = worldserver.getEntityByID(entityId);
entity.serverPosX=MathHelper.floor_double(x);
entity.posY=MathHelper.floor_double(x);
entity.posZ=MathHelper.floor_double(x);
worldserver.spawnEntityInWorld(entity);
	}
	public static void spawnEntity(int id,double x,double y,double z){
		ByteArrayOutputStream bos = new ByteArrayOutputStream(110);
		DataOutputStream dos = new DataOutputStream(bos);
		try {
			dos.writeInt(PacketId.ENTITY_SPAWN);
			dos.writeInt(id);
			dos.writeDouble(x);
			dos.writeDouble(y);
			dos.writeDouble(z);
			
			
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		Packet250CustomPayload pkt = new Packet250CustomPayload();
		pkt.channel = DefultValue.channel;
		pkt.data = bos.toByteArray();
		pkt.length = bos.size();
		pkt.isChunkDataPacket = false;
		PacketDispatcher.sendPacketToServer(pkt);

	}

}
